<?php

/**
 * album actions.
 *
 * @package    iEasy
 * @subpackage album
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class albumActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
      $this->recordCount=$request->getParameter('recordCount');
      $this->currentPage=$request->getParameter('currentPage');
      if(null==$this->recordCount||""==$this->recordCount)
          $this->recordCount=20;
      if(null==$this->currentPage||""==$this->currentPage)
          $this->currentPage=1;

      $query = Doctrine_Query::create()->from("PhotoGroup p");
            $catalogId=$request->getParameter("catalogId");
      $subCatalogId=$request->getParameter("subCatalogId");
      if($subCatalogId>0)
          $query = $query->where('p.sub_catalog_id='.$subCatalogId);
      if($catalogId>0)
          $query = $query->addWhere('p.catalog_id='.$catalogId);
      //->Where('catalog_id='.$this->catalogId)
      //->andWhere('sub_catalog_id='.$this->subCatalogId)
      $query=$query->orderBy('p.create_time desc,p.photo_group_id desc');
      $this->pages = new sfDoctrinePager('PhotoGroup',$this->recordCount);//$this->currentPage
      $this->pages->setQuery($query);  //$q   数据库查询语句
      $this->pages->setPage($this->currentPage);//
      $this->pages->init();
  }

    public function executeEdit(sfWebRequest $request)
    {
        $execSql=$request->getParameter("albumeEdit");
        $this->results="";
        $this->focTrue="false";
        $this->recTrue="false";
        $this->topTrue="false";

        switch($execSql){
            case "1"://加载
                $content=$request->getParameter("content");
                $this->focusId="";
                if(!empty($content))
                {
                    $editPhotoGroup=@Doctrine_Core::getTable('PhotoGroup')->find($content);
                    if(!empty($editPhotoGroup)){
                        if($editPhotoGroup->getIsFocusB())
                            $this->focTrue=true;
                        if($editPhotoGroup->getIsRecommendB())
                            $this->recTrue=true;
                        if($editPhotoGroup->getIsOnTopB())
                            $this->topTrue=true;
                        $this->photoGroup=$editPhotoGroup;//"s";//@$editVideo->delete();
                        if($this->photoGroup->getIsFocusB())
                        {
                           $focuLine= @Doctrine_Query::create()->from("FocusContent f")
                                    ->Where('content_id='.$this->photoGroup->getPhotoGroupId())
                                    ->andWhere('content_type_i=1003')->execute();
                            foreach($focuLine as $foc)
                            {
                                $this->focusId=@$foc->getFocusContentId();
                                break;
                            }
                        }
                        $this->photos= @Doctrine_Query::create()->from("Photo p")
                                    ->Where('p.photo_group_id='.$this->photoGroup->getPhotoGroupId())
                                    ->orderBy('p.order_i asc')->execute();
                    }
                }
                break;
            case "2"://修改
                $content=$request->getParameter("content");
                if(!empty($content))
                {
                    $photoGroup=@Doctrine_Core::getTable('PhotoGroup')->find($content);
                    if(!empty($photoGroup))
                    {
                        $photoGroup->setCatalogId($request->getParameter("sCatalogId"));
                        $titles=$request->getParameter("title");
                        $photoGroup->setTitle($titles);
                        $photoGroup->setTag($request->getParameter("tag"));
                        $photoGroup->setKeyword($request->getParameter("keyword"));
                        $photoGroup->setSnapshot($request->getParameter("shotHid"));
                        $focus=$request->getParameter("ckFocusContent");
                        $recommend=$request->getParameter("ckRecommendContent");
                        $tops=$request->getParameter("ckTop");
                        if($focus)
                            $photoGroup->setIsFocusB($focus);
                        else
                            $photoGroup->setIsFocusB(0);
                        if($recommend)
                            $photoGroup->setIsRecommendB($recommend);
                        else
                            $photoGroup->setIsRecommendB(0);
                        if($tops)
                            $photoGroup->setIsOnTopB($tops);
                        else
                            $photoGroup->setIsOnTopB(0);
                        $photoGroup->setIsHeaderB(0);
                        $urls="";
                        $indexs=$request->getParameter("firstPhoto");
                        if(1==$indexs)
                            $urls=$request->getParameter("Txt1");
                        else
                            $urls=$request->getParameter("Txt0");
                        $photoGroup->setSnapshot($urls);
                        $photoGroup->setIsAuditB($request->getParameter("auditing"));
                        $photoGroup->setModifyTime(date("Y-m-d H:i:s"));
                        $photoGroup->setPublishTime($request->getParameter("nowtime"));
                        $photoGroup->setHitI($request->getParameter("hit"));
                        $photoGroup->setHitMaskI($request->getParameter("hitMark"));
                        $photoGroup->setSource($request->getParameter("sources"));
                        $capletter=@utility::getfirstchar($titles);
                        $photoGroup->setCapitalLetter($capletter);
                        $photoGroup->save();
                        $groupId=$photoGroup->getPhotoGroupId();
                        if($groupId>=1000)
                        {
                            if($focus)
                            {
                                $focusContent=new FocusContent();
                                $focuLine= Doctrine_Query::create()->from("FocusContent f")
                                 ->Where('content_id='.$groupId)
                                 ->andWhere('content_type_i=1003')->execute();
                                foreach($focuLine as $foc)
                                {
                                    if($foc->getFocusContentId()>0)
                                        $focusContent=@Doctrine_Core::getTable('FocusContent')->find($foc->getFocusContentId());
                                    break;
                                }
                                $focusContent->setContentId($photoGroup->getPhotoGroupId());
                                $focusContent->setContentTypeI(1003);
                                $focusContent->setCatalogId($photoGroup->getCatalogId());
                                $focusContent->setModifyTime(date("Y-m-d H:i:s"));
                                $focusContent->setIsOnTopB($tops);
                                $focusContent->setSubTitle($request->getParameter("txtFocusTitle"));
                                $focusContent->setShortWord($request->getParameter("txtFocusBrief"));
                                $focusContent->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                                $focusContent->save();
                            }else
                            {
                                //删除焦点
                                $focuLine= Doctrine_Query::create()->from("FocusContent f")
                                        ->Where('content_id='.$groupId)
                                        ->andWhere('content_type_i=1003')->execute();
                                foreach($focuLine as $foc)
                                {
                                    if($foc->getFocusContentId()>0)
                                    {
                                        $focusContent=@Doctrine_Core::getTable('FocusContent')->find($foc->getFocusContentId());
                                        @$focusContent->delete();
                                    }
                                }
                            }
                            if($recommend)
                            {
                                $recomendContent=new RecommendContent();
                                $recomLine= Doctrine_Query::create()->from("RecommendContent r")
                                        ->Where('r.content_id='.$groupId)
                                        ->andWhere('r.content_type_i=1003')->execute();
                                foreach($recomLine as $recom)
                                {
                                    if($recom->getRecommendContentId()>0)
                                        $recomendContent=@Doctrine_Core::getTable('RecommendContent')->find($recom->getRecommendContentId());
                                    break;
                                }
                                $recomendContent->setCatalogId($photoGroup->getCatalogId());
                                $recomendContent->setContentId($photoGroup->getPhotoGroupId());
                                $recomendContent->setModifyTime(date("Y-m-d H:i:s"));
                                $recomendContent->setContentTypeI(1003);
                                $recomendContent->setIsOnTopB($tops);
                                $recomendContent->save();
                            }else
                            {
                                $recomLine= Doctrine_Query::create()->from("RecommendContent r")
                                        ->Where('r.content_id='.$groupId)
                                        ->andWhere('r.content_type_i=1003')->execute();
                                foreach($recomLine as $recom)
                                {
                                    if($recom->getRecommendContentId()>0)
                                    {
                                        $recomendContent=@Doctrine_Core::getTable('RecommendContent')->find($recom->getRecommendContentId());
                                        @$recomendContent->delete();
                                    }
                                }
                            }

                            $editphotos= @Doctrine_Query::create()->from("Photo p")
                                    ->Where('p.photo_group_id='.$groupId)
                                    ->orderBy('p.order_i asc')->execute();
                            foreach($editphotos as $photo)
                            {
                                @$photo->delete();
                            }
                            for($i=1;$i<=10;$i++){
                                $photo=new Photo();
                                if($i==1&&1==$indexs)
                                    $hids=$urls;
                                else
                                {
                                    $hids=$request->getParameter("Txt".$i);
                                    if(null==$hids||""==$hids)break;
                                }
                                $photo->setLink($hids);
                                $photo->setBrief($request->getParameter("Des".$i));
                                $photo->setPhotoGroupId($groupId);
                                $photo->setOrderI($i);
                                $photo->save();
                            }
                        }
                    }
                }
                $this->forward("album","index");
                break;
        }
    }

  public function executeAdd(sfWebRequest $request)
  {
      $this->execSql=$request->getParameter("albumeEdit");
      $this->results="";
      switch($this->execSql){
          case "1":
              $photoGroup=new PhotoGroup();
              $photoGroup->setCatalogId($request->getParameter("sCatalogId"));
              $titles=$request->getParameter("title");
              $photoGroup->setTitle($titles);
              $photoGroup->setTag($request->getParameter("tag"));
              $photoGroup->setKeyword($request->getParameter("keyword"));

              $focus=$request->getParameter("ckFocusContent");
              $recommend=$request->getParameter("ckRecommendContent");
              $tops=$request->getParameter("ckTop");
              if($focus)
                  $photoGroup->setIsFocusB($focus);
              else
                  $photoGroup->setIsFocusB(0);
              if($recommend)
                  $photoGroup->setIsRecommendB($recommend);
              else
                  $photoGroup->setIsRecommendB(0);
              if($tops)
                  $photoGroup->setIsOnTopB($tops);
              else
                  $photoGroup->setIsOnTopB(0);
              $photoGroup->setIsHeaderB(0);
              $urls="";
              $indexs=$request->getParameter("firstPhoto");
              if(1==$indexs)
                 $urls=$request->getParameter("Txt1");
              else
                  $urls=$request->getParameter("Txt0");
              $photoGroup->setSnapshot($urls);
              $photoGroup->setIsAuditB($request->getParameter("auditing"));
              $photoGroup->setCreateTime(date("Y-m-d H:i:s"));
              $photoGroup->setModifyTime(date("Y-m-d H:i:s"));
              $photoGroup->setPublishTime($request->getParameter("nowtime"));
              $photoGroup->setHitI(0);
              $photoGroup->setHitMaskI($request->getParameter("hitMark"));
              $photoGroup->setSource($request->getParameter("sources"));
              $capletter=@utility::getfirstchar($titles);
              $photoGroup->setCapitalLetter($capletter);
              $photoGroup->save();
              $groupId=$photoGroup->getPhotoGroupId();

              if($groupId>=1000)
              {
                  if($focus)
                  {
                      $focusContent=new FocusContent();
                      $focusContent->setContentId($photoGroup->getPhotoGroupId());
                      $focusContent->setContentTypeI(1003);
                      $focusContent->setCatalogId($photoGroup->getCatalogId());
                      $focusContent->setCreateTime(date("Y-m-d H:i:s"));
                      $focusContent->setModifyTime(date("Y-m-d H:i:s"));
                      $focusContent->setIsOnTopB($tops);
                      $focusContent->setSubTitle($request->getParameter("txtFocusTitle"));
                      $focusContent->setShortWord($request->getParameter("txtFocusBrief"));
                      $focusContent->setFocusPhoto($request->getParameter("txtFocusLocalFile"));
                      $focusContent->save();
                  }
                  if($recommend)
                  {
                      $recomendContent=new RecommendContent();
                      $recomendContent->setCatalogId($photoGroup->getCatalogId());
                      $recomendContent->setContentId($photoGroup->getPhotoGroupId());
                      $recomendContent->setCreateTime(date("Y-m-d H:i:s"));
                      $recomendContent->setModifyTime(date("Y-m-d H:i:s"));
                      $recomendContent->setContentTypeI(1003);
                      $recomendContent->setIsOnTopB($tops);
                      $recomendContent->save();
                  }
                  
                  for($i=1;$i<=10;$i++){
                      $photo=new Photo();
                      if($i==1&&1==$indexs)
                         $hids=$urls;
                      else
                      {
                          $hids=$request->getParameter("Txt".$i);
                          if(null==$hids||""==$hids)break; 
                      }
                      $photo->setLink($hids);
                      $photo->setBrief($request->getParameter("Des".$i));
                      $photo->setPhotoGroupId($groupId);
                      $photo->setOrderI($i);
                      $photo->save();
                      
                  }
              }
              $this->forward("album","index");
              
              break;

      }
  }
}
